package com.example.theorygame.layer;
/**
 * 多次游戏
 */

import android.view.MotionEvent;

import com.example.theorygame.util.CommouUtils;
import com.example.theorygame.util.DecideFactory;

import org.cocos2d.actions.instant.CCCallFunc;
import org.cocos2d.actions.interval.CCMoveBy;
import org.cocos2d.actions.interval.CCSequence;
import org.cocos2d.nodes.CCLabel;
import org.cocos2d.nodes.CCLabel.TextAlignment;
import org.cocos2d.nodes.CCSprite;
import org.cocos2d.nodes.CCTextureCache;
import org.cocos2d.opengl.CCTexture2D;
import org.cocos2d.types.CGPoint;
import org.cocos2d.types.CGRect;
import org.cocos2d.types.CGSize;

public class ThirdLayer extends BaseLayer {
    /**
     * 变量定义区域
     */
    private CCSprite blueMan, redMan, back, res;
    private CCLabel label_1, label_2, label_3, label_5;
    private String label_1Text, label_2Text, label_3Text, label_5Text;
    private String resStr;
    private CCSprite cheat, cooperste;
    private CCSprite blueface, redface;
    private CCSprite bluehat, redhat;
    //定义点击次数
    private int clicknum = 0;
    //得分
    private int score, otherscore;
    //我的总分
    public int myscore = 0;

    private int score1, score2, score3, score4, score5;
    //钱
    private CCSprite money1, money2;
    //得分框
    private CCSprite scoreBox, scoreCompare;
    private String scoreCompareString;
    //我的选则    1为合作，0为背叛
    private int mychiose, youchiose;
    //判断比赛结果  1 合合 2 合欺 3 欺合 4欺欺
    private int judgeCompare;

    private int upchiose;


    private CCSequence moveRight1, moveLeft1, moveRight2, moveLeft2, moveRight3, moveLeft3, moveRight4, moveLeft4, jump1, jump5;
    private CCSequence moveRight5, moveLeft5, moveRight6, moveLeft6, moveRight7, moveLeft7, moveRight8, moveLeft8;
    private CCMoveBy move_right_up = CCMoveBy.action(0.1f, ccp(8, 8));
    private CCMoveBy move_right_down = CCMoveBy.action(0.1f, ccp(8, -8));
    private CCMoveBy move_left_up = CCMoveBy.action(0.1f, ccp(-8, 8));
    private CCMoveBy move_left_down = CCMoveBy.action(0.1f, ccp(-8, -8));
    private CCMoveBy jump_down = CCMoveBy.action(0.1f, ccp(0, -10));
    private CCMoveBy jump_up = CCMoveBy.action(0.1f, ccp(0, 10));

    private DecideFactory decide = new DecideFactory();

    //新页面
    private CCLabel df;
    private String str;
    private CCSprite button;


    /**
     * 构造方法
     */
    public ThirdLayer() {
        //设置背景
        CCSprite bg = CCSprite.sprite("image/bg.png");
        bg.setAnchorPoint(0, 0);
        this.addChild(bg, 0);
        /**顶部*/
        //设置第1行
        label_1Text = "这次，我们动真格的啦！ 接下来，你将与五名不同的玩家进行游戏，每个人都";
        CGSize dimensions = CGSize.make(winSize.width, 100);
        label_1 = CCLabel.makeLabel(label_1Text, dimensions, TextAlignment.CENTER, "DroidSans", 10);
        label_1.setColor((ccc3(21, 21, 21)));
        label_1.setPosition(winSize.width * 0.5f, winSize.height - 20);
        addChild(label_1, 1);
        //设置第2行
        label_2Text = "有他们自己的「游戏策略」。每个玩家会与你对战三到七轮，你并不知道哪一轮";
        label_2 = CCLabel.makeLabel(label_2Text, dimensions, TextAlignment.CENTER, "DroidSans", 10);
        label_2.setColor((ccc3(21, 21, 21)));
        label_2.setPosition(winSize.width * 0.5f, winSize.height - 35);
        addChild(label_2, 1);
        //设置第3行
        label_3Text = "是这一局的最后一轮。你可以相信他们吗？或者说……他们可以相信你吗？";
        label_3 = CCLabel.makeLabel(label_3Text, dimensions, TextAlignment.CENTER, "DroidSans", 10);
        label_3.setColor((ccc3(21, 21, 21)));
        label_3.setPosition(winSize.width * 0.5f, winSize.height - 50);
        addChild(label_3, 1);
        /**底部*/
        //设置第5行
        label_5Text = "来，告诉我你的第一个真实的选择。要想起清楚喔";
        label_5 = CCLabel.makeLabel(label_5Text, dimensions, TextAlignment.CENTER, "DroidSans", 15);
        label_5.setColor((ccc3(21, 21, 21)));
        label_5.setPosition(winSize.width * 0.5f, 85);
        addChild(label_5, 1);

        //设置箱子
        back = CCSprite.sprite("image/iterated/iterated_machine.png");
        back.setAnchorPoint(0, 0);
        back.setPosition(30, 70);
        back.setScale(0.3f);
        this.addChild(back, 1);
        //设置比分
        resStr = "image/new/iterated_payoffs_4.png";
        res = CCSprite.sprite(resStr);
        res.setScale(0.3f);
        res.setAnchorPoint(0, 0);
        res.setPosition(163, 99);
        this.addChild(res, 1);
        //红色小人 你
        redMan = CCSprite.sprite("image/layer3/iterated_peep_1.png");
        redMan.setScale(0.4f);
        redMan.setAnchorPoint(0, 0);
        redMan.setPosition(10, 85);
        this.addChild(redMan, 2);
        //红色表情
        redface = CCSprite.sprite("image/new/iterated_peep_2.png");
        redface.setAnchorPoint(0, 0);
        redface.setPosition(7, 29);
        redMan.addChild(redface, 3);
        //红色帽子
        redhat = CCSprite.sprite("image/layer3/iterated_peep_13.png");
        redhat.setAnchorPoint(0, 0);
        redhat.setPosition(10, 147);
        redMan.addChild(redhat, 3);
        //蓝色小人 别人
        blueMan = CCSprite.sprite("image/layer3/iterated_peep_2.png");
        blueMan.setAnchorPoint(0, 0);
        blueMan.setPosition(360, 85);
        blueMan.setScale(0.4f);
        this.addChild(blueMan, 2);
        //蓝色表情
        blueface = CCSprite.sprite("image/new/iterated_peep_2.png");
        blueface.setFlipX(true);
        blueface.setAnchorPoint(0, 0);
        blueface.setPosition(1, 34);
        blueMan.addChild(blueface, 3);
        //蓝色帽子
        bluehat = CCSprite.sprite("image/layer3/iterated_peep_14.png");
        bluehat.setAnchorPoint(0, 0);
        bluehat.setPosition(69, 152);
        bluehat.setFlipX(true);
        blueMan.addChild(bluehat, 3);
        //设置合作按钮
        cooperste = CCSprite.sprite("image/layer3/hz.png");
        cooperste.setAnchorPoint(0, 0);
        cooperste.setScale(0.7f);
        cooperste.setPosition(240, 0);
        this.addChild(cooperste, 1);
        //设置欺骗按钮cheat
        cheat = CCSprite.sprite("image/layer3/qp.png");
        cheat.setAnchorPoint(0, 0);
        cheat.setScale(0.7f);
        cheat.setPosition(110, 0);
        this.addChild(cheat, 1);
        //设red钱
        money1 = CCSprite.sprite("image/layer3/iterated_coin.png");
        money1.setAnchorPoint(0, 0);
        money1.setPosition(185, 200);
        money1.setScale(0.7f);
        money1.setVisible(false);
        redMan.addChild(money1, 3);
        //设置blue钱
        money2 = CCSprite.sprite("image/layer3/iterated_coin.png");
        money2.setAnchorPoint(0, 1);
        money2.setPosition(0, 300);
        money2.setScale(0.7f);
        money2.setVisible(false);
        blueMan.addChild(money2, 3);
        //开启触摸
        setIsTouchEnabled(true);
    }

    /**
     * 触摸事件
     */
    @Override
    public boolean ccTouchesBegan(MotionEvent event) {
        CGPoint conver = this.convertTouchToNodeSpace(event);
        if (clicknum < 26) {
            //欺骗
            if (CGRect.containsPoint(cheat.getBoundingBox(), conver)) {
                //设置欺骗值
                upchiose = mychiose;
                mychiose = 0;
                start();
            }
            //合作
            if (CGRect.containsPoint(cooperste.getBoundingBox(), conver)) {
                //设置欺骗值
                upchiose = mychiose;
                mychiose = 1;
                start();
            }
        } else {
            //切换页面
            if (CGRect.containsPoint(button.getBoundingBox(), conver)) {
                CommouUtils.changeLayer(new FourthLayer());
            }
        }
        return true;

    }
    /*方法定义区域*/

    /**
     * 开始
     */
    public void start() {
        if (clicknum == 0) {
            scoreBox = CCSprite.sprite("image/layer3/iterated_scoreboard.png");
            scoreBox.setScale(0.6);
            scoreBox.setAnchorPoint(0, 0);
            scoreBox.setPosition(winSize.width / 2 - 45, winSize.height - 86);
            this.addChild(scoreBox, 3);
            scoreCompare = CCSprite.sprite("image/layer3/score00.png");
            scoreCompare.setScale(0.55);
            scoreCompare.setPosition(scoreBox.getPosition().x + 45, scoreBox.getPosition().y + 20);
            this.addChild(scoreCompare, 4);
        }
        //1.修改对手
        changeMan();
        //2.判断结果
        judge();
        //3.文字修改
        changeText();
        //4.比分修改
        changeScore();
        //5.点击次数加一
        clicknum += 1;

    }

    /**
     * 1.修改对手  修改blue图片  传入clicknum 传出youchiose scorel*8
     */
    public void changeMan() {
        //1.复读机
        if (clicknum < 5) {
            youchiose = decide.blueP(upchiose, clicknum % 5 + 1);
            score1 += otherscore;
        }
        //2.千年老油条 圆深蓝
        if (clicknum < 10 && clicknum >= 5) {
            CCTexture2D tex = CCTextureCache.sharedTextureCache().addImage("image/layer3/iterated_peep_15.png");
            bluehat.setTexture(tex);
            youchiose = decide.purpleP();
            score2 += otherscore;
        }
        //3.万年小粉红
        if (clicknum < 15 && clicknum >= 10) {
            CCTexture2D tex = CCTextureCache.sharedTextureCache().addImage("image/layer3/iterated_peep_16.png");
            bluehat.setTexture(tex);
            youchiose = decide.pinkP();
            score3 += otherscore;
        }
        //4.黑帮老铁 黄
        if (clicknum < 20 && clicknum >= 15) {
            CCTexture2D tex = CCTextureCache.sharedTextureCache().addImage("image/layer3/iterated_peep_17.png");
            bluehat.setTexture(tex);
            youchiose = decide.yellowP(upchiose, clicknum % 5 + 1);
            score4 += otherscore;
        }
        //5.福尔摩星儿 棕色
        if (clicknum < 25 && clicknum >= 20) {
            CCTexture2D tex = CCTextureCache.sharedTextureCache().addImage("image/layer3/iterated_peep_18.png");
            bluehat.setTexture(tex);
            youchiose = decide.brownP(upchiose, clicknum % 5 + 1);
            score5 += otherscore;
        }
        //修改界面
        if (clicknum == 25) {
            this.removeAllChildren(true);
            //背景
            CCSprite bg = CCSprite.sprite("image/layer4/layer4.png");
            bg.setAnchorPoint(0, 0);
            this.addChild(bg, 0);
            //得分
            str = Integer.toString(myscore);
            df = CCLabel.labelWithString(str, "圆体", 40);
            df.setColor(ccc3(0, 0, 0));
            df.setAnchorPoint(0, 0);
            df.setPosition(50, 240);
            this.addChild(df);
            //按钮
            button = CCSprite.sprite("image/layer4/button_next.png");
            button.setAnchorPoint(0, 0);
            button.setScale(0.8);
            button.setPosition(260, 1);
            this.addChild(button, 1);

            setIsTouchEnabled(true);
        }
    }

    /**
     * 2.判断结果         传入 mychiose youchiose  传出 judgeCompare score otherscore
     */
    public void judge() {
        if (mychiose == 1 && youchiose == 1) {
            judgeCompare = 1;
            score = 2;
            otherscore = 2;
        }
        if (mychiose == 0 && youchiose == 0) {
            judgeCompare = 4;
            score = 0;
            otherscore = 0;
        }
        if (mychiose == 1 && youchiose == 0) {
            judgeCompare = 2;
            score = -1;
            otherscore = 3;
        }
        if (mychiose == 0 && youchiose == 1) {
            judgeCompare = 3;
            score = 3;
            otherscore = -1;
        }
    }

    /**
     * 3.修改文字
     */
    public void changeText() {
        int num = (clicknum - clicknum % 5) / 5 + 1;
        int huihe = clicknum % 5 + 1;
        myscore += score;
        label_1.setString("第" + num + "/5个对手     回合：" + huihe);
        label_2.setString("你的总分：" + myscore + ":" + score1 + ":" + score2 + ":" + score3 + ":" + score4 + ":" + score5);
        label_3.removeFromParentAndCleanup(true);
        label_5.removeFromParentAndCleanup(true);
    }

    /**
     * 4.修改得分  传入 judgeCompare 传出图片
     */
    public void changeScore() {
        if (judgeCompare == 1) {
            scoreCompareString = "image/layer3/score22.png";
            System.out.println("互相合作");
            redCooperste();
            blueCooperste();
            CCTexture2D tex = CCTextureCache.sharedTextureCache().addImage("image/new/iterated_payoffs_5.png");
            res.setTexture(tex);
        }
        if (judgeCompare == 4) {
            scoreCompareString = "image/layer3/score00.png";
            System.out.println("互相欺骗");
            redCCC();
            blueCCC();
            CCTexture2D tex = CCTextureCache.sharedTextureCache().addImage("image/new/iterated_payoffs_8.png");
            res.setTexture(tex);
        }
        if (judgeCompare == 2) {
            scoreCompareString = "image/layer3/score13.png";
            System.out.println("蓝骗红");
            redBeCheat();
            blueCheat();
            CCTexture2D tex = CCTextureCache.sharedTextureCache().addImage("image/new/iterated_payoffs_7.png");
            res.setTexture(tex);
        }
        if (judgeCompare == 3) {
            scoreCompareString = "image/layer3/score31.png";
            System.out.println("红骗蓝");
            redCheat();
            blueBeCheat();
            CCTexture2D tex = CCTextureCache.sharedTextureCache().addImage("image/new/iterated_payoffs_6.png");
            res.setTexture(tex);
        }
        //1.显示(修改)得分
        CCTexture2D tex = CCTextureCache.sharedTextureCache().addImage(scoreCompareString);
        scoreCompare.setTexture(tex);
    }

    /**5.移动方法*/
    /**
     * 5.1红色欺骗
     */
    public void redCheat() {
        //执行
        money1.setVisible(true);
        moveRight1 = CCSequence.actions(move_right_up, move_right_down, move_right_up, move_right_down, CCCallFunc.action(this, "backLeft"));
        redMan.runAction(moveRight1);
        //修改面部表情
        CCTexture2D tex = CCTextureCache.sharedTextureCache().addImage("image/new/iterated_peep_8.png");
        redface.setTexture(tex);
    }

    public void backLeft() {
        CCTexture2D tex = CCTextureCache.sharedTextureCache().addImage("image/new/iterated_peep_11.png");
        redface.setTexture(tex);
        moveLeft1 = CCSequence.actions(move_left_up, move_left_down, move_left_up, move_left_down, CCCallFunc.action(this, "jumpCheatRed"));
        redMan.runAction(moveLeft1);
    }

    public void jumpCheatRed() {
        jump1 = CCSequence.actions(jump_up, jump_down, jump_up, jump_down, jump_up, jump_down, jump_up, jump_down, CCCallFunc.action(this, "moneyAwayCheat"));
        redMan.runAction(jump1);

    }

    public void moneyAwayCheat() {
        money1.setVisible(false);
        CCTexture2D tex = CCTextureCache.sharedTextureCache().addImage("image/new/iterated_peep_7.png");
        redface.setTexture(tex);
    }

    /**
     * 5.2红色合作
     */
    public void redCooperste() {
        //执行
        money1.setVisible(true);
        moveRight2 = CCSequence.actions(move_right_up, move_right_down, move_right_up, move_right_down, CCCallFunc.action(this, "moneyAwayCoo"));
        redMan.runAction(moveRight2);
        //修改面部表情
        CCTexture2D tex = CCTextureCache.sharedTextureCache().addImage("image/new/iterated_peep_8.png");
        redface.setTexture(tex);
    }

    public void moneyAwayCoo() {
        money1.setVisible(false);
        moveLeft2 = CCSequence.actions(move_left_up, move_left_down, move_left_up, move_left_down);
        redMan.runAction(moveLeft2);
        CCTexture2D tex = CCTextureCache.sharedTextureCache().addImage("image/new/iterated_peep_9.png");
        redface.setTexture(tex);
    }

    /**
     * 5.3红色被骗
     */
    public void redBeCheat() {
        money1.setVisible(true);
        moveRight3 = CCSequence.actions(move_right_up, move_right_down, move_right_up, move_right_down, CCCallFunc.action(this, "beCheat"));
        redMan.runAction(moveRight3);
        //修改面部表情
        CCTexture2D tex = CCTextureCache.sharedTextureCache().addImage("image/new/iterated_peep_8.png");
        redface.setTexture(tex);
    }

    public void beCheat() {
        money1.setVisible(false);
        CCTexture2D tex = CCTextureCache.sharedTextureCache().addImage("image/new/iterated_peep_10.png");
        redface.setTexture(tex);
        moveLeft3 = CCSequence.actions(move_left_up, move_left_down, move_left_up, move_left_down, CCCallFunc.action(this, "redSed"));
        redMan.runAction(moveLeft3);
    }

    public void redSed() {
        CCTexture2D tex = CCTextureCache.sharedTextureCache().addImage("image/new/iterated_peep_4.png");
        redface.setTexture(tex);
    }

    /**
     * 5.4红色互骗
     */
    public void redCCC() {
        money1.setVisible(true);
        moveRight4 = CCSequence.actions(move_right_up, move_right_down, move_right_up, move_right_down, CCCallFunc.action(this, "CheatCheat"));
        redMan.runAction(moveRight4);
        //修改面部表情
        CCTexture2D tex = CCTextureCache.sharedTextureCache().addImage("image/new/iterated_peep_8.png");
        redface.setTexture(tex);
    }

    public void CheatCheat() {
        money1.setVisible(false);
        CCTexture2D tex = CCTextureCache.sharedTextureCache().addImage("image/new/iterated_peep_4.png");
        redface.setTexture(tex);
        moveLeft4 = CCSequence.actions(move_left_up, move_left_down, move_left_up, move_left_down);
        redMan.runAction(moveLeft4);
    }

    /**
     * 5.5蓝色欺骗
     */
    public void blueCheat() {
        money2.setVisible(true);
        moveLeft5 = CCSequence.actions(move_left_up, move_left_down, move_left_up, move_left_down, CCCallFunc.action(this, "blueBack"));
        blueMan.runAction(moveLeft5);
        //修改面部表情
        CCTexture2D tex = CCTextureCache.sharedTextureCache().addImage("image/new/iterated_peep_8.png");
        blueface.setTexture(tex);
    }

    public void blueBack() {
        CCTexture2D tex = CCTextureCache.sharedTextureCache().addImage("image/new/iterated_peep_11.png");
        blueface.setTexture(tex);
        moveRight5 = CCSequence.actions(move_right_up, move_right_down, move_right_up, move_right_down, CCCallFunc.action(this, "blueJump"));
        blueMan.runAction(moveRight5);
    }

    public void blueJump() {
        jump5 = CCSequence.actions(jump_up, jump_down, jump_up, jump_down, jump_up, jump_down, jump_up, jump_down, CCCallFunc.action(this, "blueAway"));
        blueMan.runAction(jump5);
    }

    public void blueAway() {
        money2.setVisible(false);
        CCTexture2D tex = CCTextureCache.sharedTextureCache().addImage("image/new/iterated_peep_7.png");
        blueface.setTexture(tex);
    }

    /**
     * 5.6蓝色合作
     */
    public void blueCooperste() {
        money2.setVisible(true);
        moveLeft6 = CCSequence.actions(move_left_up, move_left_down, move_left_up, move_left_down, CCCallFunc.action(this, "blueBackCC"));
        blueMan.runAction(moveLeft6);
        //修改面部表情
        CCTexture2D tex = CCTextureCache.sharedTextureCache().addImage("image/new/iterated_peep_5.png");
        blueface.setTexture(tex);
    }

    public void blueBackCC() {
        money2.setVisible(false);
        CCTexture2D tex = CCTextureCache.sharedTextureCache().addImage("image/new/iterated_peep_9.png");
        blueface.setTexture(tex);
        moveRight6 = CCSequence.actions(move_right_up, move_right_down, move_right_up, move_right_down);
        blueMan.runAction(moveRight6);
    }

    /**
     * 5.7蓝色被骗
     */
    public void blueBeCheat() {
        money2.setVisible(true);
        moveLeft7 = CCSequence.actions(move_left_up, move_left_down, move_left_up, move_left_down, CCCallFunc.action(this, "blueBeCheating"));
        blueMan.runAction(moveLeft7);
        //修改面部表情
        CCTexture2D tex = CCTextureCache.sharedTextureCache().addImage("image/new/iterated_peep_8.png");
        blueface.setTexture(tex);
    }

    public void blueBeCheating() {
        money2.setVisible(false);
        CCTexture2D tex = CCTextureCache.sharedTextureCache().addImage("image/new/iterated_peep_10.png");
        blueface.setTexture(tex);
        moveRight7 = CCSequence.actions(move_right_up, move_right_down, move_right_up, move_right_down, CCCallFunc.action(this, "blueSad"));
        blueMan.runAction(moveRight7);
    }

    public void blueSad() {
        CCTexture2D tex = CCTextureCache.sharedTextureCache().addImage("image/new/iterated_peep_4.png");
        blueface.setTexture(tex);
    }

    /**
     * 5.8蓝色互骗
     */
    public void blueCCC() {
        money2.setVisible(true);
        moveLeft8 = CCSequence.actions(move_left_up, move_left_down, move_left_up, move_left_down, CCCallFunc.action(this, "blueCheatCheat"));
        blueMan.runAction(moveLeft8);
        //修改面部表情
        CCTexture2D tex = CCTextureCache.sharedTextureCache().addImage("image/new/iterated_peep_8.png");
        blueface.setTexture(tex);
    }

    public void blueCheatCheat() {
        money2.setVisible(false);
        CCTexture2D tex = CCTextureCache.sharedTextureCache().addImage("image/new/iterated_peep_4.png");
        blueface.setTexture(tex);
        moveRight8 = CCSequence.actions(move_right_up, move_right_down, move_right_up, move_right_down);
        blueMan.runAction(moveRight8);
    }
}


























